﻿namespace Example
{
    using System;
    using System.Net;
    using Xunit;

    public class IPAddressFacts
    {
        [Fact]
        public void IsLoopback_IPAddress_whenFalse()
        {
            Assert.False(IPAddress.IsLoopback(Dns.GetHostEntry("example.com").AddressList[0]));
        }

        [Fact]
        public void IsLoopback_IPAddress_whenTrue()
        {
            Assert.True(IPAddress.IsLoopback(IPAddress.Loopback));
        }

        [Fact]
        public void Parse_string()
        {
            const string ip = "192.0.32.10";

            Assert.Equal(ip, IPAddress.Parse(ip).ToString());
        }

        [Fact]
        public void Parse_stringEmpty()
        {
            Assert.Throws<FormatException>(() => IPAddress.Parse(string.Empty).ToString());
        }

        [Fact]
        public void Parse_stringInvalid()
        {
            Assert.Throws<FormatException>(() => IPAddress.Parse("not an IP address").ToString());
        }

        [Fact]
        public void Parse_stringNull()
        {
            // throws an ArgumentNullException
        }

        [Fact]
        public void TryParse_stringEmpty_IPAddress()
        {
            IPAddress ip;

            Assert.False(IPAddress.TryParse(string.Empty, out ip));
        }

        [Fact]
        public void TryParse_stringInvalid_IPAddress()
        {
            IPAddress ip;

            Assert.False(IPAddress.TryParse("not an IP address", out ip));
        }

        [Fact]
        public void TryParse_stringNull_IPAddress()
        {
            // throws an ArgumentNullException
        }

        [Fact]
        public void TryParse_string_IPAddress()
        {
            IPAddress ip;

            Assert.True(IPAddress.TryParse("192.0.32.10", out ip));
        }

        [Fact]
        public void prop_Any()
        {
            Assert.Equal("0.0.0.0", IPAddress.Any.ToString());
        }

        [Fact]
        public void prop_Broadcast()
        {
            Assert.Equal("255.255.255.255", IPAddress.Broadcast.ToString());
        }

        [Fact]
        public void prop_IPv6Any()
        {
            Assert.Equal("::", IPAddress.IPv6Any.ToString());
        }

        [Fact]
        public void prop_IPv6Loopback()
        {
            Assert.Equal("::1", IPAddress.IPv6Loopback.ToString());
        }

        [Fact]
        public void prop_IPv6None()
        {
            Assert.Equal("::", IPAddress.IPv6None.ToString());
        }

        [Fact]
        public void prop_Loopback()
        {
            Assert.Equal("127.0.0.1", IPAddress.Loopback.ToString());
        }

        [Fact]
        public void prop_None()
        {
            Assert.Equal("255.255.255.255", IPAddress.None.ToString());
        }
    }
}